<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Grades extends Controller_GradeHorariaFrontEnd{
        private $cookie_timeout=10;
        private $wait_timeout=2;
        
        public function before(){
            parent::logged_in_area();
        }
        public function action_index(){
            $this->view->body->content = Smarty_View::factory("3_generate_show/generate.tpl");		
            $this->view->body->content->wait_timeout = $this->wait_timeout;		
            $this->response->body($this->view);
        }
        //called via ajax
        public function action_generate(){
            $user = Model_User::get_user();
            if(strtotime($user->get_last_modification()) >= strtotime($user->get_last_generation())){
//            if(true){
                Model_Grid::generate_user_grids($user->get_id());
            }
            //set last generate for now
            Cookie::set("lgen", time(),$this->cookie_timeout);
//            $_COOKIE['lgen'] = "generate";
//            $_COOKIE['lgen'] = time();
        }
        public function action_listar(){
            //tests if action_generate was accessed in the last 10 secs
//            if(!isset($_COOKIE['lgen'])) 
//                die("not set");
//            die("lala".$_COOKIE['lgen']);
//            if((time()- $_COOKIE['lgen']) >60 )die('maior'.(time()-$_COOKIE['lgen']));
//            if(!isset($_COOKIE["lgen"]) || (time()-$_COOKIE["lgen"])>60){
            if(!isset($_COOKIE["lgen"])){
                $this->request->redirect($this->language."grades");
            }
                
//            $this->view->body->content = 
            $report = new Report(Model_Grid::load_user_grids(Model_User::get_user()->get_id()));
            $report->cacheable(false);
            $report->set_title(__("Lista de Grades Horárias"));
            $report->set_form_action("generate_and_get_grid_list");
            $report->bind("grid_id", "Ação", array("Controller_Grades","format_generate_action"));
            $report->bind("total_classes", "Matérias");
            $report->bind("total_priority", "Prioridade Total");
            $report->bind("avg_class_priority", "Prioridade Média");
            $report->bind("total_credits", "Total de Créditos");
            
//           $this->action_generate_and_get_grid_list();
            $this->view->body->content = $report->parse();		
            $this->response->body($this->view);
        }
         public function action_generate_and_get_grid_list(){
            $report = new Report(Model_Grid::load_user_grids(Model_User::get_user()->get_id()));
            $report->cacheable(false);
            $report->set_title(__("Lista de Grades Horárias"));
            $report->set_form_action("generate_and_get_grid_list");
            $report->bind("grid_id", "Ação", array("Controller_Grades","format_generate_action"));
            $report->bind("total_classes", "Matérias");
            $report->bind("total_priority", "Prioridade Total");
            $report->bind("avg_class_priority", "Prioridade Média");
            $report->bind("total_credits", "Total de Créditos");
            die($report->parse());
        }
        public static function format_generate_action($grid_id){
            return "<button onclick='return overlay_grid($grid_id)'>Gerar!</button>";
//            return "<a href='".URL_ROOT."grademagica/generate_grid/$grid_id'><button>Gerar!</button></a>";
        }
        public function action_visualize_grid(){
            $grid = Model_Grid::load_grid_html_pre_formatted($_POST['grid_id'], Model_User::get_user()->get_id());
            $view = Smarty_View::factory("3_generate_show/visualize_grid.tpl");
            $view->grid = $grid;
            die($view->render());
        }
        public function action_visualize_course(){
            $course = Model_Course::load_course_as_assoc($_POST['course_id'], Model_User::get_user()->get_id());
            $view = Smarty_View::factory("3_generate_show/visualize_course.tpl");
            $view->course = $course;
            die($view->render());
        }
        public function action_visualize_class(){
            $class = Model_Class::load_class_as_assoc($_POST['class_id'], Model_User::get_user()->get_id(),true);
            $view = Smarty_View::factory("3_generate_show/visualize_class.tpl");
            $view->class = $class;
            die($view->render());
        }
}
